import { Body, Controller, Get, Post, Req, Request, UseGuards } from "@nestjs/common";
import { UsersService } from "./users.service";
import { UsersRegisterDto } from "./dto/users-register.dto";
import { AuthGuard } from "@nestjs/passport";
import { UsersUpdateDto } from "./dto/users-update.dto";
import { PasswordUpdateDto } from "./dto/password-update.dto";

@Controller("users")
export class UsersController {
  constructor(
    private readonly userService: UsersService
  ) {
  }

  @Post("register")
  async usersRegister(@Body() dto: UsersRegisterDto, @Req() request) {
    return this.userService.usersCreate(dto, request);
  }

  //获取用户对象
  @UseGuards(AuthGuard("jwt"))
  @Get("getUserInfo")
  async getUserInfo(@Request() req) {
    // 从请求头中取出user对象

    return req.user;
  }

  //获取账户对象
  @UseGuards(AuthGuard("jwt"))
  @Get("getAccount")
  async getAccount(@Request() req) {
    return await this.userService.getAccount(req.user);
  }

  //获取账户对象
  @UseGuards(AuthGuard("jwt"))
  @Post("updateUser")
  async updateUser(@Body() dto: UsersUpdateDto, @Request() req) {
    return await this.userService.updateUser(dto, req.user);
  }

  //修改密码
  @UseGuards(AuthGuard("jwt"))
  @Post("passwordUpdate")
  async passwordUpdate(@Body() dto: PasswordUpdateDto, @Request() req) {
    return await this.userService.passwordUpdate(dto, req.user);
  }
}